package com.zhangxx.common.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName RedisConfig
 * @Description redis配置类
 * @Author leizhang
 * Date 2021/8/20 10:53 下午
 * @Version 1.0
 **/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Value("${spring.redis.user.database}")
    private int userDB;
    @Value("${spring.redis.basic.database}")
    private int basicDB;
    @Value("${spring.redis.auction.database}")
    private int auctionDB;
    @Value("${spring.redis.default.database}")
    private int defaultDB;
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String pwd;

    public JedisConnectionFactory defaultRedisConnectionFactory(int db){
        return getJedisConnectionFactory(db, host, port, pwd);
    }

    public RedisTemplate<String, Object> defaultRedisTemplate(int db) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(defaultRedisConnectionFactory(db));
        FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        serializer.setObjectMapper(mapper);
        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(serializer);
        // Hash的key也采用StringRedisSerializer的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(serializer);
        template.afterPropertiesSet();
        return template;
    }
    private JedisConnectionFactory getJedisConnectionFactory(int dbIndex, String host, int port, String pwd) {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(dbIndex);
        redisStandaloneConfiguration.setHostName(host);
        redisStandaloneConfiguration.setPort(port);
        redisStandaloneConfiguration.setPassword(RedisPassword.of(pwd));
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }

    @Bean(name = "userRedisTemplate")
    public RedisTemplate<String,Object> userRedis() {
        return defaultRedisTemplate(userDB);
    }

    @Bean(name = "basicRedisTemplate")
    public RedisTemplate<String,Object> basicRedis() {
        return defaultRedisTemplate(basicDB);
    }

    @Bean(name = "auctionRedisTemplate")
    public RedisTemplate<String,Object> auctionRedis() {
        return defaultRedisTemplate(auctionDB);
    }

    @Bean(name = "redisTemplate")
    public RedisTemplate<String,Object> redis() {
        return defaultRedisTemplate(defaultDB);
    }
}
